package com.se;


import lombok.Data;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.*;
import java.util.concurrent.Flow;
import java.util.function.*;
import java.util.stream.Stream;

/**
 * Created by hdu_huang on 2018/11/8.
 * JSR-292 study
 */
@Data
public class InvokeTest {
    private String name;
    private int age;
    private Runnable runnable = () -> System.out.println("Hello");

    private Object[] objects;

    public static void main(String[] args) throws Exception {
        InvokeTest test = new InvokeTest();

        VarHandle handle = MethodHandles.lookup().findVarHandle(InvokeTest.class, "name", String.class);
        handle.set(test, "hello");

        System.out.println(test);

        IntBinaryOperator intAdds = (a, b) -> a + b;
        System.out.println(intAdds.applyAsInt(1, 3));

        test.runnable.run();

        VarHandle varHandle = MethodHandles.lookup().findVarHandle(InvokeTest.class, "objects", Object[].class);

        //访问数组
        VarHandle arrayVarHandle = MethodHandles.arrayElementVarHandle(Object[].class);

        Object[] objects1 = new Object[32];

        varHandle.set(test, objects1);

        arrayVarHandle.set(test.objects, 1, new Object());
        arrayVarHandle.setVolatile(test.objects, 2, "hello world!");

        Object aVolatile = arrayVarHandle.getVolatile(test.objects, 2);

    }


    public void sayHello(String content) {
        System.out.println(content);
    }
}
